package pers.cfeng.server.versionManage;

import lombok.Data;
import pers.cfeng.server.txManage.TransactionConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * TM 主要是在底层对事务文件进行操作,VM上层使用的应该是抽象的事务
 */

@Data
public class Transaction {
    private long tid;
    private int level; //隔离级别
    private Map<Long, Boolean> snapshot; //快照
    private Exception err;
    private boolean autoRollback;

    public static Transaction newTransaction(long tid, int level, Map<Long, Transaction> active) {
        Transaction transaction = new Transaction();
        transaction.tid = tid;
        transaction.level = level;
        if(level != 0) {
            transaction.snapshot = new HashMap<>();
            for(Long t:  active.keySet())
                transaction.snapshot.put(t, true);
        }
        return transaction;
    }

    //判断tid是否在当前的snap中
    public boolean isInSnapshot(long tid) {
        if(tid == TransactionConstant.NONE_TID)
            return false;
        return snapshot.containsKey(tid);
    }
}
